ComponentOne BarCode for UWP
Quick Start / Step 2 of 3: Adding Code
In This Topic
    Step 2 of 3: Adding Code
    In This Topic

    In this step, you will add the code needed for your application.

    1. Add the following namespace to the top of your page:
      C#
      Copy Code
      using C1.BarCode;
      
    2.  Register a MainPage_Loaded event directly below the InitializeComponent() method. Your code should resemble the following:
      C#
      Copy Code
      this.InitializeComponent();
      this.Loaded += MainPage_Loaded;
      
    3. Next, add a MainPage_Loaded event:
      C#
      Copy Code
      void MainPage_Loaded(object sender, RoutedEventArgs e)
      {
          cbCodeType.ItemsSource = Enum.GetValues(typeof(CodeType));
          cbCodeType.SelectedItem = barcode.CodeType;
      }
      
    4. By adding a SelectionChanged event, when you change the type of barcode you are displaying, the code will check to see what type of barcode is being displayed. Based on the type of barcode, the application will either show or hide the image:
      C#
      Copy Code
      private async void cbCodeType_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          try
          {
              if (barcode != null &&
                  cbCodeType != null &&
                  cbCodeType.SelectedItem != null)
              {
                  barcode.CodeType = (CodeType)cbCodeType.SelectedItem;
              }
              if (barcode.CodeType != CodeType.QRCode
                          || !text.Text.Equals("http://www.componentone.com"))
              {
                  image.Opacity = 0;
              }
              else
              {
                  image.Opacity = 1;
              }
          }
          catch (Exception ex)
          {
              await new Windows.UI.Popups.MessageDialog(ex.Message, "Error").ShowAsync();
          }
      }
      
    5. The TextChanged event controls the text that is encoded in the C1BarCode control. In addition to this, if you choose a QRCode type barcode, then the image you chose will appear over the barcode:
      C#
      Copy Code
      private void text_TextChanged(object sender, TextChangedEventArgs e)
      {
          {
              if (!string.IsNullOrEmpty(text.Text) &&
                  text.Text.Equals("http://www.componentone.com") &&
                  barcode.CodeType == CodeType.QRCode)
                  image.Opacity = 1;
              else
                  image.Opacity = 0;
          }
      }
      

    In this step, you added code to control the appearance of the C1BarCode control. In the next step, you will run the application.

    See Also